Login.render   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 33
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 33
rs 9.1359
c 0
b 0
f 0
cc 4
1
/*eslint max-len: ["error", { "code": 170 }]*/
2
3
import React, { Component } from 'react';
4
import base from '../../config/api.js';
5
let api = base.api();
6
7
class Login extends Component {
8
    constructor(props) {
9
        super(props);
10
        this.registerSubmit = this.registerSubmit.bind(this);
11
        this.toggleShowPassword = this.toggleShowPassword.bind(this);
12
        this.state = {
13
            showing: false,
14
            hidden: true,
15
            button: true,
16
            invalid: false
17
        };
18
    }
19
    registerSubmit(event) {
20
        let that = this;
21
22
        event.preventDefault();
23
        const data = new FormData(event.target);
24
25
        let person = {
26
            "name": data.get('name'),
27
            "password": data.get('password')
28
        };
29
30
        fetch(api + "/login", {
31
            method: 'POST',
32
            body: JSON.stringify(person),
33
            headers: {
34
                'Content-Type': 'application/json'
35
            }
36
        })
37
            .then(res => res.json())
38
            .then(function (res) {
39
                if (res.data.result) {
40
                    localStorage.setItem("activeUser", JSON.stringify(res.data.user));
41
                    localStorage.setItem("token", JSON.stringify(res.data.token));
42
                    that.props.history.push('/profile');
43
                    window.location.reload(false);
44
                } else {
45
                    that.setState({
46
                        invalid: <p className="center invalid">{res.data.user}</p>
47
                    });
48
                }
49
            });
50
    }
51
    toggleShowPassword() {
52
        this.setState({
53
            hidden: !this.state.hidden,
54
            button: !this.state.button
55
        });
56
    }
57
    render() {
58
        let user = localStorage.getItem("activeUser");
59
60
        if (user === null) {
61
            return (
62
                <div className="form-wrapper">
63
                    <h1>Login</h1>
64
                    <p className="center">To be able to access the chat and edit reports you must first login.</p>
65
                    <form action="/profile" className="form-register" onSubmit={this.registerSubmit}>
66
                        <label className="form-label">Username
67
                            <input className="form-input" type="text" name="name" required placeholder="Your username" />
68
                        </label>
69
70
                        <label className="form-label">Password
71
                            <input
72
                                className="form-input password"
73
                                type={this.state.hidden ? "password" : "text"}
74
                                name="password"
75
                                placeholder="Your password"
76
                                required
77
                            />
78
                            <p><input type="checkbox" className="show-password" onClick={this.toggleShowPassword} /> {this.state.button ? "Show" : "Hide"} password</p>
79
                        </label>
80
                        <input className="button form-button center" type="submit" name="login" value="Login" />
81
                    </form>
82
                    {this.state.invalid}
83
                </div>
84
            );
85
        } else {
86
            return (
87
                <div className="form-wrapper">
88
                    <h1>Already logged in</h1>
89
                </div>
90
            );
91
        }
92
    }
93
}
94
95
export default Login;
96